package com.eusoft.demo_project.service;

import com.eusoft.demo_project.entity.*;
import com.eusoft.demo_project.mapper.CarManageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;


@Service
public class CarMangerServiceImpl implements CarManageService{
    @Autowired
    CarManageMapper carMangerMapper;

    @Override
    public List<Car_type> queryCarTypeList() {
        return carMangerMapper.queryCarTypeList();
    }

    @Override
    public List<Car_state> queryCarStateList() {
        return carMangerMapper.queryCarStateList();
    }

    @Override
    public Integer countCarList(Integer id, Integer stateId, String search) {
        if("".equals(search)) search =null;
        return carMangerMapper.countCarList(id, stateId, search);
    }

    @Override
    public List<Car> queryCarList(Integer id, Integer stateId, String search, Integer offset, Integer pageRows) {
        if("".equals(search)) search =null;
        return carMangerMapper.queryCarList(offset,pageRows,id,stateId,search);
    }

    @Override
    public void delCar(Integer aid) {
        carMangerMapper.deleteCarUser(aid);
        carMangerMapper.deleteCar(aid);
        System.out.println("删除成功");
    }

    @Override
    public Car queryCarDetail(Integer aid) {
        return carMangerMapper.queryCarDetail(aid);
    }

    @Override
    public Integer countCarUser(Integer aid) {
        return carMangerMapper.countCarUser(aid);
    }

    @Override
    public List<User> querycarUserList(Integer aid, Integer offset, Integer pageRows) {
        return carMangerMapper.queryCarUserList(aid,offset,pageRows);
    }

    @Override
    public Car queryCarInfo(Integer aid) {
        Car car = carMangerMapper.queryCarDetail(aid);
        car.setUserList(carMangerMapper.queryCarUserInfo(aid));
        return car;
    }

    @Override
    public List<User> queryAllUser() {
        return carMangerMapper.queryAllUser();
    }

    @Override
    public Boolean checkCarPlateCode(String plateCode,Integer aid) {
        if(aid == -1){
            List<String> codes = carMangerMapper.queryAllCarPlateCode();
            for(String code : codes){
                if(code.equals(plateCode))
                    return false;
            }
            return true;
        }
        else {
            List<String> codes = carMangerMapper.queryAllCarPlateCodeExceptById(aid);
            for(String code : codes){
                if(code.equals(plateCode))
                    return false;
            }
            return true;
        }

    }

    @Override
    //修改或新增车辆及使用用户
    public void addOrUpdateCar(Integer aid,List<Integer> user_ids, String plate_code, String brand, Integer type_id, String type_name, Integer state_id, String state_name, Integer approved_num, String memo) {
        //检查车牌号是否存在（区分修改和新增）
        if(checkCarPlateCode(plate_code,aid)) {
            //获取当前日期
            Date date = new Date();
            //aid=-1 时新增车辆以及车辆的使用用户
            if (aid == -1) {

                carMangerMapper.addCar(plate_code, brand, type_id, type_name, state_id, state_name, approved_num, memo, date, date);
                //添加车辆使用用户
                Integer car_aid = carMangerMapper.queryCarIdByPlateCode(plate_code);
                for (Integer userId : user_ids) {
                    carMangerMapper.addCarUser(car_aid, userId, date, date);
                }
            }
            //aid!=-1,修改车辆信息
            else{
                //车辆信息修改
                carMangerMapper.updateCar(aid,plate_code,brand,type_id,type_name,state_id,state_name,approved_num,memo,date);
                //根据车辆aid查询当前UserId列表
                List<Integer> currentUserIds = carMangerMapper.queryCarUserIdList(aid);
                //获取车辆使用修改前后相同部分的UserId
                List<Integer> commons = new ArrayList<>();
                for(Integer userId:user_ids){
                    for(Integer currentUserId : currentUserIds){
                        if(userId == currentUserId){
                            commons.add(userId);
                        }
                    }
                }
                System.out.println("共有的用户部分为："+commons);
                //若有相同部分，删除修改前后UserId
                if(!commons.isEmpty()){
                    //删除修改后车辆用户相同部分
                    Iterator<Integer> userIterator = user_ids.iterator();
                    while(userIterator.hasNext()){
                        Integer userId = userIterator.next();
                        for(Integer common :commons){
                            if(userId == common){
                                userIterator.remove();
                            }
                        }
                    }
                    //删除修改前车辆用户相同部分
                    Iterator<Integer> currentUserIterator = currentUserIds.iterator();
                    while(currentUserIterator.hasNext()){
                        Integer currentUserId = currentUserIterator.next();
                        for(Integer common :commons){
                            if(currentUserId == common){
                                currentUserIterator.remove();
                            }
                        }
                    }
                }

                //修改前车辆用户经删除后不为空，则删除对应记录
                if(!currentUserIds.isEmpty()){
                    for(Integer userId :currentUserIds){
                        carMangerMapper.deleteCarUserByUser(aid,userId);
                        System.out.println("删除了" + aid+ "的使用者：" + userId);
                    }
                }
                //修改后车辆用户经删除后不为空，则新增记录
                if(!user_ids.isEmpty()){
                    for(Integer userId :user_ids){
                        carMangerMapper.addCarUser(aid,userId,date,date);
                        System.out.println("增加了" + aid+ "的使用者："+ userId);
                    }
                }
            }
        }
        else{
            System.out.println("车牌号已存在");
        }

    }
}
